package com.yyk.dzvisit.interceptor;//package org.tempuri.interceptor;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.cxf.binding.soap.SoapMessage;
//import org.apache.cxf.interceptor.Fault;
//import org.apache.cxf.io.CachedOutputStream;
//import org.apache.cxf.phase.AbstractPhaseInterceptor;
//import org.apache.cxf.phase.Phase;
//import org.dom4j.Document;
//import org.dom4j.DocumentException;
//import org.dom4j.Element;
//import org.tempuri.utils.CommonUtil;
//
//import java.io.IOException;
//
///**
// * @ClassName: XmlnsInterceptor
// * @Description: TODO
// * @Author: wxl 王晓龙
// * @Date: 2020/8/14 10:37
// */
//@Slf4j
//public class XmlnsInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
//
//
//    public XmlnsInterceptor() {
//        super(Phase.PREPARE_SEND);
////        Phase.PRE_INVOKE
////        Phase.PREPARE_SEND
////        Phase.PRE_STREAM
//    }
//
//    @Override
//    public void handleMessage(SoapMessage soapMessage) throws Fault {
//
//        System.out.println("ws interceptor");
//        try {
//            //需要放置在根节点的命名空间
////            Map<String, String> envMap = new HashMap<>();
////            envMap.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
////            envMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
////            envMap.put("xsd", "http://www.w3.org/2001/XMLSchema");
////
////            //在命名空间下的元素都以自定义前缀生成
////            Map<String, String> namespaceMap = new HashMap<>();
////            namespaceMap.put("http://www.w3.org/2001/XMLSchema", "xsd");
////            namespaceMap.put("http://schemas.xmlsoap.org/soap/envelope/", "soap");
////            namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
////            JAXBDataBinding dataBinding = (JAXBDataBinding) soapMessage.getExchange().getEndpoint().getService()
////                    .getDataBinding();
////            dataBinding.setNamespaceMap(namespaceMap);
////            soapMessage.put("soap.env.ns.map", envMap);
////            soapMessage.put("disable.outputstream.optimization", true);
//            ////
////            OutputStream os = soapMessage.getContent(OutputStream.class);
////            CachedStream cs = new CachedStream();
////            soapMessage.setContent(OutputStream.class, cs);
////            soapMessage.getInterceptorChain().doIntercept(soapMessage);
////            CachedOutputStream csnew = (CachedOutputStream) soapMessage.getContent(OutputStream.class);
////            InputStream in = csnew.getInputStream();
////            String xml = IOUtils.toString(in);
////            System.out.println("11111111111111111");
////            System.out.println(xml);
////            String newXml = this.replaceAllGl(xml);
//            //这里对xml做处理，处理完后同理，写回流中
////            IOUtils.copy(new ByteArrayInputStream(newXml.getBytes()), os);
////            cs.close();
////            os.flush();
//        } catch (Exception e) {
//            log.error("cxfInterceptor error", e);
//        }
//
//        //<?xml version="1.0" encoding="utf-8"?>
//        //<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
//        //    <soap:Body>
//        //        <FWC_IF_CardCode_GetResponse xmlns="http://tempuri.org/">
//        //            <FWC_IF_CardCode_GetResult>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;xml&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;ResponseResult&gt;&lt;IsSuccess&gt;0&lt;/IsSuccess&gt;&lt;Message&gt;加载xml异常根级别上的数据无效。 第 1 行，位置 1。&lt;/Message&gt;&lt;/ResponseResult&gt;&lt;/body&gt;&lt;/xml&gt;</FWC_IF_CardCode_GetResult>
//        //        </FWC_IF_CardCode_GetResponse>
//        //    </soap:Body>
//        //</soap:Envelope>
//    }
//
//    private class CachedStream extends CachedOutputStream {
//
//        public CachedStream() {
//
//            super();
//
//        }
//
//        protected void doFlush() throws IOException {
//
//            currentStream.flush();
//
//        }
//
//        protected void doClose() throws IOException {
//
//        }
//
//        protected void onWrite() throws IOException {
//
//        }
//
//    }
//
//
//    public static void main(String[] args) throws DocumentException {
//        StringBuffer xml = new StringBuffer();
//        xml.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
//                "    <soap:Body>\n" +
//                "        <ns2:getDictionaryResponse xmlns:ns2=\"http://tempuri.org/\">\n" +
//                "            <getDictionaryResult>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?>&lt;xml>&lt;head>\n" +
//                "                        &lt;AccessToken>调用接口的授权码&lt;/AccessToken>\n" +
//                "                        &lt;OpenUserID>外部接入系统ID&lt;/OpenUserID>\n" +
//                "                        &lt;ParameterName>AccountType&lt;/ParameterName>\n" +
//                "                    &lt;/head>&lt;body>&lt;ResponseResult>&lt;IsSuccess>1&lt;/IsSuccess>&lt;Message>业务成功&lt;/Message>&lt;SubCode>200&lt;/SubCode>&lt;SubMessage>操作成功&lt;/SubMessage>&lt;/ResponseResult>&lt;DictionaryList>&lt;ParameterName>AccountType&lt;/ParameterName>&lt;CodeValue>1&lt;/CodeValue>&lt;CodeName>门诊&lt;/CodeName>&lt;Explain&gt;帐户类型&lt;/Explain>&lt;/DictionaryList>&lt;DictionaryList>&lt;ParameterName>AccountType&lt;/ParameterName>&lt;CodeValue>2&lt;/CodeValue>&lt;CodeName>住院&lt;/CodeName>&lt;Explain>帐户类型&lt;/Explain>&lt;/DictionaryList>&lt;/body>&lt;sign>&lt;/sign>&lt;/xml></getDictionaryResult>\n" +
//                "        </ns2:getDictionaryResponse>\n" +
//                "    </soap:Body>\n" +
//                "</soap:Envelope>");
//        Document document = CommonUtil.xmlToDocument(xml.toString());
////            document.selectSingleNode("")//ns2:IF_Get_Booking_ResourceResponse
//        Element rootElement = document.getRootElement();
//        Element body = rootElement.element("Body");
//        Element response = (Element) body.elements().get(0);
//        String namespaceURI = response.getNamespaceURI();
//        String responseName = response.getName();
//        Element result = (Element) response.elements().get(0);
//        String s = result.asXML();
//        int firstIndex = s.indexOf("?");
//        String pre = s.substring(0, firstIndex);
//
//
//        int lastIndex = s.lastIndexOf("<");
//        String suffix = s.substring(lastIndex);
//
//        String middle = s.substring(firstIndex, lastIndex);
//        String newMiddle = middle.replaceAll(">", "&gt;");
//        System.out.println(pre);
//
//        System.out.println(middle);
//
//        System.out.println(suffix);
//
//        System.out.println(s);
//        System.out.println(newMiddle);
//
//
////        newResponse.addElement(result);
//
//        //
//
////        Node getDictionaryResponse = document.selectSingleNode("getDictionaryResponse");
////        System.out.println(getDictionaryResponse.getName());
//
//    }
//
//
//    public String replaceAllGl(String xml) throws DocumentException {
//        Document document = CommonUtil.xmlToDocument(xml);
//        Element rootElement = document.getRootElement();
//        Element body = rootElement.element("Body");
//        Element response = (Element) body.elements().get(0);
//        Element result = (Element) response.elements().get(0);
//        String s = result.asXML();
//        int firstIndex = s.indexOf("?");
//        String pre = s.substring(0, firstIndex);
//        int lastIndex = s.lastIndexOf("<");
//        String suffix = s.substring(lastIndex);
//        String middle = s.substring(firstIndex, lastIndex);
//        String newMiddle = middle.replaceAll(">", "&gt;");
//        return pre.concat(newMiddle).concat(suffix);
//    }
//
//}
